package com.yc.cloud;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.yc.cloud.entity.BaseEntity;

import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {

    public static void main(String[] args) {
        // 数据库配置
        String url = "jdbc:mysql://localhost:3306/yccloud_metahuman?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "Acme@2023";

        // 包配置
        String parentPackage = "com.example.project";
        String dtoPath = "/dto/";
        String voPath = "/vo/";

        // 代码生成器
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("Author") // 设置作者
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(System.getProperty("user.dir") + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(parentPackage) // 设置父包名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(getTables()) // 设置需要生成的表名
                            .entityBuilder()
                            .superClass(BaseEntity.class) // 设置实体类父类
                            .addTableFills(new Column("create_time", FieldFill.INSERT)) // 设置填充字段
                            .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                            .controllerBuilder()
                            .enableRestStyle(); // 开启生成@RestController控制器
                })
                .injectionConfig(builder -> {
                    // 自定义输出配置
                    builder.beforeOutputFile((tableInfo, objectMap) -> {
                                System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap);
                            }).customMap(Collections.singletonMap("dto", dtoPath))
                            .customMap(Collections.singletonMap("vo", voPath));
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateConfig(builder -> builder.disable(TemplateType.CONTROLLER, TemplateType.SERVICE, TemplateType.SERVICE_IMPL))
                .execute();
    }

    // 获取用户输入的表名
    private static List<String> getTables() {
        System.out.println("请输入表名，多个英文逗号分隔：");
        Scanner scanner = new Scanner(System.in);
        String tables = scanner.nextLine();
        if (tables == null || tables.isEmpty()) {
            throw new RuntimeException("表名不能为空！");
        }
        return List.of(tables.split(","));
    }
}
